package com.tengju.bff.interfaces.manage.member.sign.blacklist;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

import com.tengju.bff.interfaces.manage.member.sign.blacklist.facade.StarBlacklistFacade;
import com.tengju.bff.interfaces.manage.member.sign.blacklist.form.AddStarBlacklistForm;
import com.tengju.bff.interfaces.manage.member.sign.blacklist.form.DelStarBlacklistForm;
import com.tengju.bff.interfaces.manage.member.sign.blacklist.form.EditStarBlacklistForm;
import com.tengju.bff.interfaces.manage.member.sign.blacklist.form.GetStarBlacklistForm;
import com.tengju.bff.interfaces.manage.member.sign.blacklist.vo.PageStarBlacklistVO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ManageUtil;
import com.tengju.user.application.archives.StarBlacklistService;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@Api(tags = "会员管理-签约管理-黑名单管理")
@RequestMapping(value = "manage/member/star/blacklist/")
@Slf4j
@ManageEntrance
public class StarBlacklistController {

    @Resource
    StarBlacklistService starBlackListService;
    @Resource
    StarBlacklistFacade starBlacklistFacade;

    @RequiresRoles("sso")
    @ApiOperation(value = "添加黑名单", notes = "添加黑名单")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> addArticle(@RequestBody AddStarBlacklistForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            starBlackListService.add(form.getIdCode(),
                    form.getLimitStarLevel(),
                    staff.getName());
            return ApiResponse.newSuccess();
        } catch (DomainException | ApplicationException e) {
            log.error("添加黑名单失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("添加黑名单异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "修改黑名单", notes = "修改黑名单")
    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> editArticle(@RequestBody EditStarBlacklistForm form) {
        try {
            Staff staff = ManageUtil.getOperateUser();
            starBlackListService.edit(form.getIdCode(), form.getLimitStarLevel(), staff.getName());
            return ApiResponse.newSuccess();
        } catch (DomainException | ApplicationException e) {
            log.error("修改黑名单失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("修改黑名单异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "删除黑名单", notes = "删除黑名单")
    @RequestMapping(value = "del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> delArticle(@RequestBody DelStarBlacklistForm form) {
        try {
            starBlackListService.del(form.getIdCode());
            return ApiResponse.newSuccess();
        } catch (DomainException | ApplicationException e) {
            log.error("删除黑名单失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("删除黑名单异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @RequiresRoles("sso")
    @ApiOperation(value = "分页查询黑名单", notes = "分页查询黑名单")
    @RequestMapping(value = "getByPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<PageStarBlacklistVO> getStarBlackListByPage(GetStarBlacklistForm form) {
        try {
            return ApiResponse.newSuccess(starBlacklistFacade.getStarBlackListByPage(form));
        } catch (DomainException | ApplicationException e) {
            log.error("分页查询黑名单失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("分页查询黑名单异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }
}
